package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.LearnCourse;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.service.CourseService;
import com.wxxymaker.edu.service.LearnCourseService;
import com.wxxymaker.edu.utils.TokenUtils;
import io.github.biezhi.anima.page.Page;

/**
 * @AUTHOR soft
 * @DATE 2018/9/15 20:52
 * @DESCRIBE 加入学习的课程相关接口
 */
@StudentRoles
@Path(value = "/course/learn/api", restful = true)
public class CourseLearnHandlerApi {
    @Inject
    private LearnCourseService learnCourseService;
    @Inject
    private CourseService courseService;

    /**
     * 获取用户正在学习的所有课程
     * @param uid 用户id 如果不存在则获取当前登录用户的所有学习课程
     * @param over 是否学完
     */
    @PostRoute("/page")
    public RestResponse pageLearnCourse(@Param(defaultValue = "0") Integer uid,
                                        @Param Boolean over,
                                        @Param(defaultValue = "1") int page,
                                        @Param(defaultValue = "10") int size) {
        User user = TokenUtils.currentUser();
        if (uid == 0 && user != null) uid = TokenUtils.currentUser().getId();
        if (uid > 0) {
            Page<LearnCourse> page1 = learnCourseService.page(uid, over, page, size, true);
            if (page1 != null) {
                return RestResponse.ok(page1);
            }
            return RestResponse.fail("无任何学习课程!");
        }
        return RestResponse.fail("无查询用户！");
    }

    /**
     * 通过课程id 获取当前用户的学习进度信息
     * 带着下一节课
     * @param kid 课程id
     * @param jc  是否加入课程实体 默认不加入
     */
    @PostRoute("/one")
    public RestResponse find(@Param int kid,
                             @Param boolean jc) {
        if (courseService.isPublish(kid)) {
            Integer uid = TokenUtils.currentUser().getId();
            LearnCourse learnCourse = learnCourseService.byKidAndUid(kid, uid, jc);
            if (learnCourse != null) {
                return RestResponse.ok(learnCourse);
            }
            return RestResponse.fail("你可能并没有学习过该课程!");
        }
        return RestResponse.fail("该课程还没有发布!");
    }

    /**
     * 将课程加入学习
     * @param kid 需要加入学习的课程id
     */
    @PostRoute("/:kid/join")
    public RestResponse joinLearn(@PathParam Integer kid) {
        Integer     uid  = TokenUtils.currentUser().getId();
        if (!courseService.isMyPublish(kid, uid)) {
            if (!learnCourseService.isJoin(kid, uid)) {
                LearnCourse join = learnCourseService.join(kid, uid);
                if (join != null) {
                    courseService.addStudents(kid); // 增加学习量
                    return RestResponse.ok(join);
                }
                return RestResponse.fail("添加学习失败！");
            }
            return RestResponse.fail("正在学习中,无须重复加入!");
        }
        return RestResponse.fail(302, "自己发布的课程还要加入学习吗?!!");
    }

    /**
     * 退出课程学习
     * @param kid 课程id
     */
    @PostRoute("/:kid/unjoin")
    public RestResponse unjoinLearn(@PathParam Integer kid) {
        Integer uid = TokenUtils.currentUser().getId();
        if (!courseService.isMyPublish(kid, uid)) {
            if (learnCourseService.isJoin(kid, uid)) {
                if (learnCourseService.unjoin(kid, uid)) {
                    courseService.reduceStudents(kid); // 减少学习量
                    return RestResponse.ok("退出学习成功！");
                }
            }
            return RestResponse.fail("你并没有学习该课程，退啥？！！");
        } else {
            return RestResponse.fail("这是你发布的课程，直接返回就行！");
        }
    }
}
